package com.shopnight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopnight.entity.Image;
import com.shopnight.entity.RankedPrize;
import com.shopnight.mapper.ImageMapper;
import com.shopnight.mapper.ShopOrderMapper;
import com.shopnight.service.RankedPrizeService;
import com.shopnight.mapper.RankedPrizeMapper;
import com.shopnight.vo.PrizesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shopnight.vo.PrizesVO.Order;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 13201
* @description 针对表【ranked_prize】的数据库操作Service实现
* @createDate 2022-11-11 18:25:16
*/
@Service
public class RankedPrizeServiceImpl extends ServiceImpl<RankedPrizeMapper, RankedPrize>
    implements RankedPrizeService{

    @Autowired
    ShopOrderMapper shopOrderMapper;

    @Autowired
    ImageMapper imageMapper;

    @Override
    public List<PrizesVO> getRankedPrize() {
        List<RankedPrize> rankedPrizes = this.baseMapper.selectList(null);

        return  rankedPrizes.stream().map( rankedPrize -> {
                    PrizesVO prizesVO = new PrizesVO();
                    BeanUtils.copyProperties(rankedPrize,prizesVO);
                    if (rankedPrize.getPrizeType() ==1 ){
                        prizesVO.setOrder( getOrder(rankedPrize.getPrizeProperty()) );
                    }else {
                        prizesVO.setCoupon(null/*getCoupon(rankedPrize.getPrizeProperty())*/);
                    }
                    return prizesVO;
                }).collect(Collectors.toList());

    }

    //根据商品id获取名字与默认图片
    public Order getOrder(Integer prizeProperty){
        Order order = new Order();
        order.setOrderName(shopOrderMapper.getOrderById(prizeProperty).getOrderName());
        order.setDefaultImgUrl(imageMapper.selectOne(
                new LambdaQueryWrapper<Image>()
                        .eq(Image::getOrderId,prizeProperty)
                        .eq(Image::getDefaultImg,1)
        ).getImgUrl());
        return order;
    }

}




